import { resolveToken } from '../utils'

interface User {
  id: number
  name: string
  avatar: string
  email?: string
  roles: string[]
  permissions: string[]
}

const users: { [key: string]: User } = {
  admin: {
    id: 1,
    name: 'Ag(admin)',
    avatar: 'https://gravatar.com/userimage/236118802/819ac4929dea8322c7507759cfdc4057.jpeg?size=256',
    email: 'Ronnie@123.com',
    roles: ['admin'],
    permissions: ['*:*:*']
  },
  editor: {
    id: 2,
    name: 'Ag(editor)',
    avatar: 'https://gravatar.com/userimage/236118802/819ac4929dea8322c7507759cfdc4057.jpeg?size=256',
    email: 'Ronnie@123.com',
    roles: ['editor'],
    permissions: ['premi-test:index:edit']
  },
  guest: {
    id: 3,
    name: '访客(guest)',
    avatar: 'https://gravatar.com/userimage/236118802/819ac4929dea8322c7507759cfdc4057.jpeg?size=256',
    roles: [],
    permissions: []
  }
}

export interface UserApiResponse {
  code: number
  data: User
}

const userApiRoutes: {
  url: string
  method: string
  response: ({ headers }: { headers?: any }) => UserApiResponse
}[] = [
  {
    url: '/api/user',
    method: 'get',
    response: ({ headers }) => {
      const token = resolveToken(headers?.authorization)
      return {
        code: 0,
        data: {
          ...(users[token] || users.guest)
        }
      }
    }
  }
]

export default userApiRoutes
